local yanxing = fk.CreateSkill {
  name = "th_yanxing",
  tags = { Skill.Compulsory },
}

local ThU = require "packages/2hu/pkg/util/th_util"
yanxing:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.extra_data and move.extra_data.th_yanxing_source then
        return true
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local ret = {}
    for _, move in ipairs(data) do
      if move.extra_data and move.extra_data.th_yanxing_source then
        for _, arr in ipairs(move.extra_data.th_yanxing_source) do
          local cardid = arr[1]
          if room:getCardArea(cardid) == Card.DiscardPile then
            table.insertIfNeed(ret, arr)
          end
        end
      end
    end
    for _, cidarr in ipairs(ret) do
      event:setSkillData(self, "cancel_cost", false)

      if not player:hasSkill(yanxing.name) then break end
      event:setCostData(self, cidarr)
      self:doCost(event, target, player, data)

      event:setSkillData(self, "cancel_cost", false)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    room:obtainCard(player, dat[1], true, fk.ReasonJustMove)
    local choices = {}
    for _, skill in ipairs(player:getSkillNameList()) do
      if player:hasSkill(skill) then
        table.insert(choices, skill)
      end
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = yanxing.name,
      prompt = "#th_yanxing-choose",
      detailed = true
    })
    if choice then
      room:invalidateSkill(player, choice, "-turn", yanxing.name)
    end
  end
})
yanxing:addEffect(fk.BeforeCardsMove, {
  can_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        return true
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        local ret = {}
        if move.from == nil and move.moveReason == fk.ReasonUse or move.moveReason == fk.ReasonResponse then
          local eType = move.moveReason == fk.ReasonUse and GameEvent.UseCard or GameEvent.RespondCard
          local move_event = room.logic:getCurrentEvent()
          local use_event = move_event.parent
          if use_event ~= nil and use_event.event == eType then
            local use = use_event.data
            if use and use.card and use.card.name == "fire__slash" then
              local user = use.from
              local card_ids = room:getSubcardsByRule(use.card)
              for _, info in ipairs(move.moveInfo) do
                if table.contains(card_ids, info.cardId) then
                  table.insert(ret, {info.cardId, user.id})
                end
              end
            end
          end
        else
          for _, info in ipairs(move.moveInfo) do
            local from = move.from or ThU.getCardLastOwner(room, info.cardId)
            Fk:filterCard(info.cardId, from)
            local card = Fk:getCardById(info.cardId)
            if card.name == "fire__slash" then
              table.insert(ret, {info.cardId, (from or Util.DummyTable).id})
            end
          end
        end
        if next(ret) then
          move.extra_data = move.extra_data or {}
          move.extra_data.th_yanxing_source = ret
        end
      end
    end
  end
})

Fk:loadTranslationTable{
  ["th_yanxing"] = "焰星",
  [":th_yanxing"] = "锁定技，火【杀】或一名角色使用或打出的火【杀】进入弃牌堆后，你获得之并令你的一个技能本回合失效。",

  ["#th_yanxing-choose"] = "焰星：你须令你的一个技能本回合失效",
}

return yanxing